package com.sudoku.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.sudoku.utility.Constants;
import com.sudoku.utility.Locator;

public class LoadingAssetsScreen implements Screen, Constants {

	private boolean render;
	private BitmapFont font;
	private SpriteBatch batch;
	private ShapeRenderer loadBar;
	private ShapeRenderer loadBarOutline;
	private Stage stage;

	public LoadingAssetsScreen() {
		batch = new SpriteBatch();
		render = true;
		loadBar=new ShapeRenderer();
		loadBarOutline=new ShapeRenderer();
		FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(FONT));
	    FreeTypeFontParameter param=new FreeTypeFontParameter();
	    param.size=(int)(60);
	    font = generator.generateFont(param);
	    font.setColor(Color.RED);
	    generator.dispose();
	    
	    stage=new Stage(Locator.getViewport());
	    
		loadBarOutline.setTransformMatrix(stage.getCamera().view);
		loadBar.setTransformMatrix(stage.getCamera().view);
		batch.setTransformMatrix(stage.getCamera().view);
		batch.setProjectionMatrix(stage.getCamera().projection);
		loadBarOutline.setProjectionMatrix(stage.getCamera().projection);
		loadBar.setProjectionMatrix(stage.getCamera().projection);
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		ShapeRenderer sh = new ShapeRenderer();
		sh.begin(ShapeType.Filled);
		sh.setColor(0, 0, 0, 1);
		sh.rect(0, 0, ACTUAL_VIRTUAL_WIDTH, ACTUAL_VIRTUAL_HEIGHT);
		sh.end();
		
		stage.act(delta);
		stage.draw();
		
		if (render && Locator.getAssets().update()) {
			Locator.getGame().init(); //init the starting screens with the assets
			render = false;
		}
		/*renderLoadBar();
		if (render) {
			batch.begin();
			String s=String.valueOf((int)(Locator.getAssets().getProgress() * 100)) + " %";
			font.draw(batch, "Loading "+s, ACTUAL_VIRTUAL_WIDTH * 0.39f, ACTUAL_VIRTUAL_HEIGHT * 0.70f);
			batch.end();
		}*/
	}

	private void renderLoadBar() {
		loadBarOutline.begin(ShapeType.Line);
		loadBarOutline.rect(ACTUAL_VIRTUAL_WIDTH*0.20f, ACTUAL_VIRTUAL_HEIGHT*0.05f, ACTUAL_VIRTUAL_WIDTH*0.60f, ACTUAL_VIRTUAL_HEIGHT*0.10f);
		loadBarOutline.end();
		
		loadBar.begin(ShapeType.Filled);
		loadBar.rect(ACTUAL_VIRTUAL_WIDTH*0.20f, ACTUAL_VIRTUAL_HEIGHT*0.05f, ACTUAL_VIRTUAL_WIDTH*0.60f*Locator.getAssets().getProgress(), ACTUAL_VIRTUAL_HEIGHT*0.10f);
		loadBar.end();
		
	}

	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
